package com.wang.myblog.service.impl;

import com.wang.myblog.common.PageResponse;
import com.wang.myblog.controller.common.UploadController;
import com.wang.myblog.entity.Article;
import com.wang.myblog.entity.ArticleTag;
import com.wang.myblog.entity.Kind;
import com.wang.myblog.entity.Tag;
import com.wang.myblog.mapper.ArticleMapper;
import com.wang.myblog.mapper.ArticleTagMapper;
import com.wang.myblog.mapper.TagMapper;
import com.wang.myblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Override
    public boolean insert(Tag tag) {
        if (tagMapper.insert(tag) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 删除标签。将关联文章标签置空并放入回收站
     * @param id 主键
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {
        Tag tag=tagMapper.queryById(id);
        if (tagMapper.deleteById(id) == 1) {
            UploadController.deleteCommonImg(tag.getImg());
            List<Article> list=articleMapper.queryArticleByTagId(id);
            articleTagMapper.deleteByTagId(id);
            list.forEach(article -> {
                //当文章没有关联其他标签时放入回收站
                if (articleTagMapper.queryByArticleId(article.getId()).size()==0){
                    article.setStatus(2);
                    articleMapper.update(article);
                }
            });
            return true;
        }
        return false;
    }

    /**
     * @param id 主键
     * @return 实例对象
     * @Description 查询单条数据
     */
    @Override
    public Tag queryById(Integer id) {
        return tagMapper.queryById(id);
    }

    /**
     * @return 对象列表
     * @Description 查询全部数据
     * 分页使用MyBatis的插件实现
     */
    @Override
    public List<Tag> queryAll() {
        List<Tag> tags=tagMapper.queryAll();
        tags.forEach(tag -> {
            tag.setArticleCount(articleTagMapper.getArticleCount(tag.getId()));
        });
        return tags;
    }

    @Override
    public List<Tag> queryAll(Integer status) {
        List<Tag> tags=tagMapper.queryAll();
        tags.forEach(tag -> {
            tag.setArticleCount(articleTagMapper.getArticleCountByStatus(tag.getId(),status));
        });
        return tags;
    }


    /**
     * @param tag 实例对象
     * @return 对象列表
     * @Description 实体作为筛选条件查询数据
     */
    @Override
    public List<Tag> queryAll(Tag tag) {
        return tagMapper.queryAll(tag);
    }

    /**
     * @param tag 实例对象
     * @return 是否成功
     * @Description 修改数据，哪个属性不为空就修改哪个属性
     */
    @Override
    public boolean update(Tag tag) {
        Tag tag1=tagMapper.queryById(tag.getId());
        if (tagMapper.update(tag) == 1) {
            if(!tag.getImg().equals(tag1.getImg())){
                UploadController.deleteCommonImg(tag1.getImg());
            }
            return true;
        }
        return false;
    }

    /**
     * @return java.util.List<com.moti.entity.Tag>
     * @Description 根据文章ID查询所有标签
     **/
    @Override
    public List<Tag> queryByArticleId(Integer id) {
        return articleTagMapper.queryByArticleId(id);
    }

    /**
     * @return java.lang.Integer
     * @Description 根据标签ID获取关联文章的数量
     **/
    @Override
    public Integer getArticleCount(Integer id) {
        return articleTagMapper.getArticleCount(id);
    }

    /**
     * @return java.lang.Integer
     * @Description 获取标签的数量
     **/
    @Override
    public Integer getCount() {
        return tagMapper.getCount();
    }

    @Override
    public Tag queryByName(String name) {
        return tagMapper.queryByName(name);
    }

    @Override
    public PageResponse queryByPageAndSearchMsg(Integer draw, Integer start, Integer limit, String searchMsg) {
        List<Tag> tagList=tagMapper.queryByPageAndSearchMsg(start,limit,searchMsg);
        tagList.forEach(tag -> {
            tag.setArticleCount(articleTagMapper.getArticleCount(tag.getId()));
        });
        Integer total= tagMapper.getCountBySearchMsg(searchMsg);
        return new PageResponse(draw,total,total,tagList);
    }

    @Override
    public String getTagNameList() {
        String[] nameList=new String[tagMapper.getCount()];
        List<Tag> tags= tagMapper.queryAll();
        for(int i=0;i<tags.size();i++){
            nameList[i]=tags.get(i).getName();
        }
        String result="[";
        for (int k=0;k < nameList.length;k++){
            result += '"'+nameList[k]+'"'+",";
        }
        return result+"]";
    }

    @Override
    public String getTagArticleReadCount() {
        Integer[] counts=new Integer[tagMapper.getCount()];
        List<Tag> tags= tagMapper.queryAll();
        for(int i=0;i<tags.size();i++){
            List<Article> articles=articleMapper.queryArticleByTagId(tags.get(i).getId());
            Integer a=0;
            for (Article article:articles){
                a +=article.getReadCount();
            }
            counts[i]=a;
        }
        return Arrays.toString(counts);
    }


}
